const express = require('express');
const router = express.Router();
const {query} = require('../models/db');


router.get('/', async (req, res, next) => {
    res.render('admin/index', {
        layout: 'layout-admin'

    });
});

router.get('/channel', async (req, res, next) => {
    res.render('admin/channel', {
        layout: 'layout-admin',
        nav: 'channel'
    });
});

const multer = require('multer');
const storage = multer.diskStorage({
    destination: function (req, file, cb) {//存储目录
        cb(null, 'src/img');
    },
    filename: function (req, file, cb) {
        let extname = '';
        switch (file.mimetype) {
            case 'image/jpg':
                extname = '.jpg';
                break;
            case 'image/png':
                extname = '.png';
                break;
            case 'image/gif':
                extname = '.gif';
                break;
        }
        cb(null, Date.now() + extname);
    }
});
const upload = multer({
    storage,
    limits: {fileSize: 2 * 1024 * 1024},// 2M
    fileFilter: function (req, file, cb) {//判断文件是否合法
        if (file.mimetype === "image/gif" || file.mimetype === "image/jpg" || file.mimetype === "image/png") {
            cb(null, true);
        } else {
            cb(new err('请上传图片格式'), false);
        }

    }
});
router.get('/live', (req, res, next) => {
    res.render('admin/live', {
        layout: 'layout-admin',
        liveadd:true,
        nav: 'live'
    });
});
const {body, validationResult} = require('express-validator/check');
const validations = [
    body('name').not().isEmpty().withMessage('标题必填'),
    body('lTime').not().isEmpty().withMessage('时间必填').isAfter(new Date().toString()).withMessage('不能晚于当前时间  '),
];

// ...是脱壳 ↑↑↑
router.post('/live', [upload.single('img'), ...validations]/*同时执行多个中间键*/, async (req, res, next) => {
    const errors = validationResult(req).formatWith(({msg}) => msg);// ({msg})=>msg   是(err)=>err.mgs简略写法
    if (errors.isEmpty()) {
        //保存用户名
        req.body.img = req.file ? req.file.filename : '';
        //保存数据
        try {
            const result = await query(`INSERT INTO live SET ?`, req.body);
            const message = result.affectedRows > 0 ? '添加成功' : '添加失败';
            res.render('admin/result', {
                layout: 'layout-admin',
                message
            });
        } catch (err) {
            console.log(err);
        }
    } else {
        res.render('admin/result', {
            layout: 'layout-admin',
            message: '新增失败',
            errors: errors.array(),
        });
    }


});

router.get('/footer', async (req, res, next) => {
    res.render('admin/footer', {
        layout: 'layout-admin',
        nav: 'footer'

    });
});


router.get('/video', async (req, res, next) => {
    res.render('admin/video', {
        layout: 'layout-admin',
        nav: 'video'

    });
});

router.get('/prog', (req, res, next) => {
    res.render('admin/prog', {
        layout: 'layout-admin',
        nav: 'prog'
    });
});

router.post('/prog', async (req, res, next) => {

});

module.exports = router;